package lfm.mpr;

import java.util.ArrayList;

import objects.Jornada;
import objects.Liga;
import db.AdminBaseDatosHelper;
import adapter.AdapterListJornadas;
import adapter.AdapterListMyLigas;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class JornadasActivity extends Activity {
	
	 private boolean customTitleSupported;
	 private EditText search_box;
	 private ListView listview_jornadas;
	 private AdminBaseDatosHelper miBBDDHelper;
	 private AdapterListJornadas adapter_jornadas;
	 private ArrayList<Jornada> list_jornadas;
	 
	 public void onCreate(Bundle savedInstanceState) {
		 
	        super.onCreate(savedInstanceState);
	        customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	        setContentView(R.layout.jornadas);
	        customTitleBar(getText(R.string.app_name).toString());
	        
	        search_box = (EditText) findViewById(R.id.search_box);
	        listview_jornadas = (ListView) findViewById(R.id.listview_jornadas);
	        
	        listview_jornadas.setFastScrollEnabled(true); 
	        int[] colors = {0, 0xFFFF0000, 0}; // red for the example
	        listview_jornadas.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
	        listview_jornadas.setDividerHeight(3);
	        
	        miBBDDHelper = new AdminBaseDatosHelper(JornadasActivity.this);
	        list_jornadas = miBBDDHelper.Get_Jornadas_BD();
	        adapter_jornadas = new AdapterListJornadas(JornadasActivity.this, list_jornadas);
	        listview_jornadas.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	        listview_jornadas.setAdapter(adapter_jornadas);
	        adapter_jornadas.notifyDataSetChanged();
	        
	        search_box.setOnKeyListener(new OnKeyListener() {
	    	      public boolean onKey(View v, int keyCode, KeyEvent event) {
	    	          if (keyCode  == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
	    	        	  
	    	        	  adapter_jornadas.getFilter().filter(search_box.getText().toString());
	    	        	  InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
	    	        	  imm.hideSoftInputFromWindow(search_box.getWindowToken(), 0);
	    	        	  return true;
	    	          }
	    	          else return false;
	    	      }
	    	  });
	        
	        
	 }//end_onCreate
	 
	 
	 public void customTitleBar(String left) {
			
			//Miramos las preferencias de usuario si existen
			SharedPreferences prefs = getSharedPreferences("MisPreferencias",Context.MODE_PRIVATE);
	        String user = prefs.getString("user", "lfm");
	        
	        if (user.length() > 20) user = user.substring(0, 20);
	        // set up custom title
	        if (customTitleSupported) {
	                getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titleapp);
	                TextView titleTvLeft = (TextView) findViewById (R.id.left_text);
	                titleTvLeft.setText(user);
	                titleTvLeft.setTextColor(Color.WHITE);
	        }
		}//end_customTitleBar

}
